<template>
    <el-form ref="ruleFormRef" :rules="rules" :model="formNews" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="标题" prop="newsTitle">
            <el-input v-model="formNews.newsTitle" placeholder="请输入标题"/>
          </el-form-item>
        </el-col>

        <!-- 发布时间 -->
    

        <el-col :span="24">
          <el-form-item label="内容" prop="newsContent">
            <el-input  type="textarea" v-model="formNews.newsContent" placeholder="请输入内容"/>
          </el-form-item>
        </el-col>
    
      </el-row>
    </el-form>
    
      <div class="dialog-button-wrap">
        <el-button @click="close">取消</el-button>
        <el-button @click="addNews(ruleFormRef)" type="success" :loading="subLoading" color="#083a6d">保存</el-button>
      </div>
    
    </template>
    
    <script setup lang="ts">
    import {ref,reactive } from 'vue'
    import { ElMessage,FormInstance,FormRules } from 'element-plus'
    import {addNewsApi} from "@/api/edu/news/news";
    // 表单实例对象
    const ruleFormRef = ref<FormInstance>()
    // 表单约束规则对象
    const rules = reactive<FormRules>({
      newsTitle: [{ required: true,message: '标题不能为空',trigger: 'blur' }],
      newsContent: [{ required: true,message: '内容不能为空',trigger: 'blur' }]
    })
    // 定义事件
    const emit = defineEmits(['closeAddNewsForm','success'])
    // 按钮状态
    const subLoading = ref(false)
    // 表单数据对象
    const formNews = reactive({
      newsTitle: '',
      newsContent:'',
    })
    
    // 新增新闻信息
    const addNews = async (formEl:FormInstance | undefined)=> {
      if(!formEl)return
      await formEl.validate(async (valid,fields)=> {
        subLoading.value = true
        if(valid){
          const { data } = await addNewsApi(formNews)
          if (data.status === 200){
            ElMessage.success(data.message)
            emit('success')
          }else {
            ElMessage.error(data.message)
          }
        }else {
          ElMessage.error('提交失败，你还有未填写的项')
        }
      })
      subLoading.value = false
    }
    // 取消表单
    const close = ()=> {
      emit('closeAddNewsForm')
    }
    </script>
    
    <style scoped>
    .dialog-button-wrap {
      text-align: center;
      margin-top: 20px;
    }
    </style>
    